package com.hirsi.common.core.validator;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.hirsi.common.core.constant.CommonConstant;
import com.hirsi.common.core.validator.annotation.Captcha;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 图形验证码规则实现
 * @author 封夕
 * @since 2023/6/7 17:28
 */
public class CaptchaValidator implements ConstraintValidator<Captcha, String> {
    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        return StrUtil.isNotBlank(s) && Validator.isMatchRegex("^[A-Za-z0-9]+$", s) && s.length() == CommonConstant.CAPTCHA_LENGTH;
    }
}
